package com.projectmanagement.server.service.serviceImpl;


import com.alibaba.fastjson.JSON;
import com.projectmanagement.server.dao.entity.bo.GanttObject;
import com.projectmanagement.server.dao.entity.bo.PageObject;
import com.projectmanagement.server.dao.entity.po.CaseDetail;
import com.projectmanagement.server.dao.entity.po.TaskPlan;
import com.projectmanagement.server.dao.mapper.CaseDetailMapper;
import com.projectmanagement.server.dao.mapper.PlanManageMapper;
import com.projectmanagement.server.service.enums.UserRoleEnum;
import com.projectmanagement.server.service.utils.DateUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @description
 * @author 星空梦语
 * @date 2021/3/9 下午4:08
 */
@Service
public class StatisticsService {

    @Resource
    private PlanManageMapper planManageMapper;

    @Resource
    private CaseDetailMapper caseDetailMapper;


    /**
     * @description
     * @author 星空梦语
     * @date 2021/3/9 下午4:08
     */
    public String queryAll(PageObject pageObject) {
        int offset = (pageObject.getOffset() - 1) * pageObject.getLimit();
        List<Map> mapList = new ArrayList<>();
        Map pageMap= (Map) pageObject.getData();
        int role= Integer.parseInt(pageMap.get("role").toString());
        int count=planManageMapper.selectAll().size();
        List<TaskPlan> taskPlanList = planManageMapper.queryPageData(offset, pageObject.getLimit(), pageMap.get("queryProjectText").toString(),null);
        for (TaskPlan taskPlan : taskPlanList) {
            List<CaseDetail> caseDetailList = caseDetailMapper.queryTaskByExample(taskPlan.getId());
            String task = taskPlan.getPlanName();

            //统计总数据
            int all_count = caseDetailList.size();
            if(all_count<=2) all_count=1;
            int all_run_count;
            double all_runSuccess_count,all_runFaiL_count;
            if(role== UserRoleEnum.TESTER.getFlag()){
                all_run_count = (int) caseDetailList.stream().filter(item -> !item.getTesterRunResult().equals("")).count();
                all_runSuccess_count = caseDetailList.stream().filter(item -> item.getTesterRunResult().equals("通过")).count();
                all_runFaiL_count = (int) caseDetailList.stream().filter(item -> item.getTesterRunResult().equals("失败")).count();
            }else{
                all_run_count = (int) caseDetailList.stream().filter(item -> !item.getRdRunResult().equals("")).count();
                all_runSuccess_count = caseDetailList.stream().filter(item -> item.getRdRunResult().equals("通过")).count();
                all_runFaiL_count = (int) caseDetailList.stream().filter(item -> item.getRdRunResult().equals("失败")).count();
            }

            //统计p1数据
            List<CaseDetail> p1 = caseDetailList.stream().filter(item -> item.getCasePriority().equals("1")).collect(Collectors.toList());
            int all_p1 = p1.size();
            double p1_run_count,p1_success_count,p1_fail_count;
            if(role== UserRoleEnum.TESTER.getFlag()) {
                p1_run_count = (int) p1.stream().filter(item -> !item.getTesterRunResult().equals("")).count();
                p1_success_count = (int) p1.stream().filter(item -> item.getTesterRunResult().equals("通过")).count();
                p1_fail_count = (int) p1.stream().filter(item -> item.getTesterRunResult().equals("失败")).count();
            }else{
                p1_run_count = (int) p1.stream().filter(item -> !item.getRdRunResult().equals("")).count();
                p1_success_count = (int) p1.stream().filter(item -> item.getRdRunResult().equals("通过")).count();
                p1_fail_count = (int) p1.stream().filter(item -> item.getRdRunResult().equals("事变")).count();
            }

            //统计p2数据
            List<CaseDetail> p2 = caseDetailList.stream().filter(item -> item.getCasePriority().equals("2")).collect(Collectors.toList());
            int all_p2 = p2.size();
            double p2_run_count,p2_success_count,p2_fail_count;
            if(role== UserRoleEnum.TESTER.getFlag()) {
                p2_run_count = (int) p2.stream().filter(item -> !item.getTesterRunResult().equals("")).count();
                p2_success_count = (int) p2.stream().filter(item -> item.getTesterRunResult().equals("通过")).count();
                p2_fail_count = (int) p2.stream().filter(item -> item.getTesterRunResult().equals("失败")).count();
            }else{
                p2_run_count = (int) p2.stream().filter(item -> !item.getRdRunResult().equals("")).count();
                p2_success_count = (int) p2.stream().filter(item -> item.getRdRunResult().equals("通过")).count();
                p2_fail_count = (int) p2.stream().filter(item -> item.getRdRunResult().equals("失败")).count();
            }

            //统计p3数据
            List<CaseDetail> p3 = caseDetailList.stream().filter(item -> item.getCasePriority().equals("3")).collect(Collectors.toList());
            int all_p3 = p3.size();
            double p3_run_count,p3_success_count,p3_fail_count;
            if(role== UserRoleEnum.TESTER.getFlag()) {
                p3_run_count = (int) p3.stream().filter(item -> !item.getTesterRunResult().equals("")).count();
                p3_success_count = (int) p3.stream().filter(item -> item.getTesterRunResult().equals("通过")).count();
                p3_fail_count = (int) p3.stream().filter(item -> item.getTesterRunResult().equals("失败")).count();
            }else{
                p3_run_count = (int) p3.stream().filter(item -> !item.getRdRunResult().equals("")).count();
                p3_success_count = (int) p3.stream().filter(item -> item.getRdRunResult().equals("通过")).count();
                p3_fail_count = (int) p3.stream().filter(item -> item.getRdRunResult().equals("失败")).count();
            }

            //统计p4数据
            List<CaseDetail> p4 = caseDetailList.stream().filter(item -> item.getCasePriority().equals("4")).collect(Collectors.toList());
            int all_p4 = p4.size();
            double p4_run_count,p4_success_count,p4_fail_count;
            if(role== UserRoleEnum.TESTER.getFlag()) {
                p4_run_count = (int) p4.stream().filter(item -> !item.getRdRunResult().equals("")).count();
                p4_success_count = (int) p4.stream().filter(item -> item.getTesterRunResult().equals("通过")).count();
                p4_fail_count = (int) p4.stream().filter(item -> item.getTesterRunResult().equals("失败")).count();
            }else{
                p4_run_count = (int) p4.stream().filter(item -> !item.getRdRunResult().equals("")).count();
                p4_success_count = (int) p4.stream().filter(item -> item.getRdRunResult().equals("通过")).count();
                p4_fail_count = (int) p4.stream().filter(item -> item.getRdRunResult().equals("失败")).count();
            }



            Map map = new HashMap();
            map.put("task", task);
            map.put("allCase", all_count);
            map.put("allRunCase", all_run_count);
            map.put("allRate", all_count==0?"0%":parse(all_run_count,all_count));
            map.put("allSuccessRate", all_run_count==0?"0%":parse(all_runSuccess_count,all_run_count));
            map.put("allFailRate", all_run_count==0?"0%":parse(all_runFaiL_count,all_run_count) );
            map.put("p1Count", all_p1);
            map.put("p1RunCount", p1_run_count);
            map.put("p1SuccessRate", all_p1==0?"0%":parse(p1_success_count,all_p1) );
            map.put("p1FailRate", all_p1==0?"0%":parse(p1_fail_count,all_p1) );
            map.put("p2Count", all_p2);
            map.put("p2RunCount", p2_run_count);
            map.put("p2SuccessRate", all_p2==0?"0%":parse(p2_success_count,all_p2) );
            map.put("p2FailRate", all_p2==0?"0%":parse(p2_fail_count,all_p2) );
            map.put("p3Count", all_p3);
            map.put("p3RunCount", p3_run_count);
            map.put("p3SuccessRate", all_p3==0?"0%":parse(p3_success_count,all_p3) );
            map.put("p3FailRate", all_p3==0?"0%":parse(p3_fail_count,all_p3) );
            map.put("p4Count", all_p4);
            map.put("p4RunCount", p4_run_count);
            map.put("p4SuccessRate", all_p4==0?"0%":parse(p4_success_count,all_p4)  );
            map.put("p4FailRate",all_p4==0?"0%": parse(p4_fail_count,all_p4) );
            mapList.add(map);
        }


        Map<String, Object> mapdata = new HashMap<String, Object>();
        mapdata.put("total", count);
        mapdata.put("rows", mapList);
        return JSON.toJSONString(mapdata);
    }

    /**
     * @description
     * @author 星空梦语
     * @date 2021/3/9 下午4:08
     */
    public String parse(double n1,double n2){
        DecimalFormat df = new DecimalFormat("0.00");//格式化小数
        double data= Double.parseDouble(df.format(n1/n2))*100;
        DecimalFormat df2 = new DecimalFormat("0");//格式化小数
        return df2.format(data)+"%";
    }


    /**
     * @description
     * @author 星空梦语
     * @date 2021/3/9 下午4:08
     * @return
     */
    public double parse2(double n1, double n2){
        DecimalFormat df = new DecimalFormat("0.0");//格式化小数
        return Double.parseDouble(df.format(n1/n2));
    }


    /**
     * @description 查询甘道图数据
     * @author 星空梦语
     * @date 2021/3/9 下午4:08
     */
    public String queryGanttData(PageObject pageObject) throws Exception {
        Map pageMap= (Map) pageObject.getData();
        int role= Integer.parseInt(pageMap.get("role").toString());
        List<GanttObject> ganttObjectList=new ArrayList<>();
        List<TaskPlan> taskPlanList = planManageMapper.selectAll();
        for (TaskPlan taskPlan : taskPlanList) {
            List<CaseDetail> caseDetailList = caseDetailMapper.queryTaskByExample(taskPlan.getId());
            String task = taskPlan.getPlanName();
            //统计总数据
            int all_count = caseDetailList.size();
            int all_run_count=0;
            if(role== UserRoleEnum.TESTER.getFlag()) {
                all_run_count = (int) caseDetailList.stream().filter(item -> !item.getTesterRunResult().equals("")).count();
            }else{
                all_run_count = (int) caseDetailList.stream().filter(item -> !item.getRdRunResult().equals("")).count();
            }

//            if(parse2(all_run_count,all_count)<=1){ //排除掉已经执行完毕的任务
                GanttObject ganttObject=new GanttObject();
                ganttObject.setText(task);
                ganttObject.setStart_date(DateUtil.formatToDayByDate5(taskPlan.getStartTime()));
                ganttObject.setProgress(parse2(all_run_count,all_count));
                ganttObject.setId(taskPlan.getId());
                ganttObject.setTester(taskPlan.getTester());
                ganttObject.setDeveloper(taskPlan.getDeveloper());
                ganttObject.setDuration(DateUtil.daysBetween2(taskPlan.getStartTime(),taskPlan.getEndTime()));
                ganttObjectList.add(ganttObject);
//            }

        }
        return JSON.toJSONString(ganttObjectList);
    }


}
